package com.rabbiter.ordering.mapper;

import com.rabbiter.ordering.entity.DishCategory;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface DishCategoryMapper {

    // 根据ID查询分类
    @Select("SELECT * FROM dish_category WHERE id = #{id}")
    DishCategory selectById(@Param("id") Integer id);

    // 查询所有分类列表
    @Select("SELECT * FROM dish_category ORDER BY sort ASC")
    List<DishCategory> selectAll();

    // 查询启用的分类列表
    @Select("SELECT * FROM dish_category WHERE status = 1 ORDER BY sort ASC")
    List<DishCategory> selectEnabled();

    // 条件查询分类
    @Select("<script>" +
            "SELECT * FROM dish_category " +
            "<where>" +
            "   <if test='name != null'>AND name LIKE CONCAT('%', #{name}, '%')</if>" +
            "   <if test='status != null'>AND status = #{status}</if>" +
            "</where>" +
            "ORDER BY sort ASC" +
            "</script>")
    List<DishCategory> selectByCondition(DishCategory condition);

    // 新增分类
    @Insert("INSERT INTO dish_category(name, description, sort, status) " +
            "VALUES(#{name}, #{description}, #{sort}, #{status})")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    int insert(DishCategory category);

    // 更新分类
    @Update("UPDATE dish_category SET " +
            "name = #{name}, " +
            "description = #{description}, " +
            "sort = #{sort}, " +
            "status = #{status} " +
            "WHERE id = #{id}")
    int update(DishCategory category);

    // 删除分类
    @Delete("DELETE FROM dish_category WHERE id = #{id}")
    int deleteById(@Param("id") Integer id);

    // 检查分类名称是否已存在
    @Select("SELECT COUNT(*) FROM dish_category WHERE name = #{name} AND id != #{excludeId}")
    int countByName(@Param("name") String name, @Param("excludeId") Integer excludeId);

    // 更新分类状态
    @Update("UPDATE dish_category SET status = #{status} WHERE id = #{id}")
    int updateStatus(@Param("id") Integer id, @Param("status") Boolean status);

    // 获取分类总数
    @Select("SELECT COUNT(*) FROM dish_category")
    int count();
}
